package procedure;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author sk.z
 */
public class Find {

    //最深分流点-各独立流程遍历查找
    public static Node deepestFork(Node process) {
        List<List<Node>> allSingleLine = travelGetAllSingleLine(process);
        Node fork = null;
        int deep = -1;
        for (List<Node> line : allSingleLine) {
            for (int i = 0; i < line.size(); i++) {
                if (line.get(i).getAfterList().size() > 1 && i > deep) {
                    fork = line.get(i);
                    deep = i;
                }
            }
        }
        return fork;
    }

    //遍历获取全部单线流程
    public static List<List<Node>> travelGetAllSingleLine(Node process) {
        List<List<Node>> result = new ArrayList<>();
        LinkedList<Node> link = new LinkedList<>();
        travel(process, link, result);
        return result;
    }

    //遍历获取全部单线流程 递归方法
    private static void travel(Node curNode, LinkedList<Node> link, List<List<Node>> result) {
        //终点记录单流程
        if (curNode.getAfterList().isEmpty()) {
            List<Node> singleLine = new ArrayList<>(link);
            singleLine.add(curNode);
            result.add(singleLine);
            return;
        }

        link.addLast(curNode);
        for (Node n : curNode.getAfterList()) {
            travel(n, link, result);
        }
        link.removeLast();
    }

}
